home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / rsz-minibuf.el.z / rsz-minibuf.el
Encoding:
Text File  |  1998-05-21  |  8.4 KB  |  234 lines

  1. ;;; rsz-minibuf.el --- dynamically resize minibuffer to display entire contents
  2.  
  3. ;; Copyright (C) 1990 Roland McGrath
  4. ;; Copyright (C) 1993, 1994 Noah S. Friedman
  5.  
  6. ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
  7. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  8. ;; Modified for Lucid Emacs By: Peter Stout <pds@cs.cmu.edu>
  9. ;; Maintainer: friedman@prep.ai.mit.edu
  10. ;; Keywords: minibuffer, window, frames, display
  11.  
  12. ;; This file is part of XEmacs.
  13.  
  14. ;; XEmacs is free software; you can redistribute it and/or modify it
  15. ;; under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; XEmacs is distributed in the hope that it will be useful, but
  20. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. ;; General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with XEmacs; see the file COPYING.  If not, you can either
  26. ;; send email to this program's maintainer or write to: The Free
  27. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  28. ;; 02111-1307, USA.
  29.  
  30. ;;; Synched up with: Not synched.
  31.  
  32. ;;; Commentary:
  33.  
  34. ;; This file has received maintenance by the XEmacs development team.
  35.  
  36. ;; $Id: rsz-minibuf.el,v 1.9 1994/04/08 09:18:59 friedman Exp $
  37.  
  38. ;; This package allows the entire contents (or as much as possible) of the
  39. ;; minibuffer to be visible at once when typing.  As the end of a line is
  40. ;; reached, the minibuffer will resize itself.  When the user is done
  41. ;; typing, the minibuffer will return to its original size.
  42.  
  43. ;; In window systems where it is possible to have a frame in which the
  44. ;; minibuffer is the only window, the frame itself can be resized.  In FSF
  45. ;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
  46. ;; its original size after it ceases to be active because
  47. ;; `minibuffer-exit-hook' didn't exist until version 19.23.
  48.  
  49. ;; NOTE: The code to resize frames has not been tested under Lucid Emacs,
  50. ;; because detached minibuffers are broken.
  51.  
  52. ;; Note that the minibuffer and echo area are not the same!  They simply
  53. ;; happen to occupy roughly the same place on the frame.  Messages put in
  54. ;; the echo area will not cause any resizing by this package.
  55.  
  56. ;; This package is considered a minor mode but it doesn't put anything in
  57. ;; minor-mode-alist because this mode is specific to the minibuffer, which
  58. ;; has no modeline.
  59.  
  60. ;; To use this package, put the following in your .emacs:
  61. ;;
  62. ;;     (autoload 'resize-minibuffer-mode "rsz-minibuf" nil t)
  63. ;;
  64. ;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
  65.  
  66. ;;; Code:
  67.  
  68.  
  69.  
  70. (defgroup resize-minibuffer nil
  71.   "Dynamically resize minibuffer to display entire contents"
  72.   :group 'frames)
  73.  
  74.  
  75. (defcustom resize-minibuffer-mode nil
  76.   "*If non-`nil', resize the minibuffer so its entire contents are visible."
  77.   :type 'boolean
  78.   :require 'rsz-minibuf
  79.   :group 'resize-minibuffer)
  80.  
  81. (defcustom resize-minibuffer-window-max-height nil
  82.   "*Maximum size the minibuffer window is allowed to become.
  83. If less than 1 or not a number, the limit is the height of the frame in
  84. which the active minibuffer window resides."
  85.   :type '(choice (const nil) integer)
  86.   :group 'resize-minibuffer)
  87.  
  88. (defcustom resize-minibuffer-window-exactly t
  89.   "*If non-`nil', make minibuffer exactly the size needed to display all its contents.
  90. Otherwise, the minibuffer window can temporarily increase in size but
  91. never get smaller while it is active."
  92.   :type 'boolean
  93.   :group 'resize-minibuffer)
  94.  
  95.  
  96. (defcustom resize-minibuffer-frame nil
  97.   "*If non-`nil' and the active minibuffer is the sole window in its frame, allow changing the frame height."
  98.   :type 'boolean
  99.   :group 'resize-minibuffer)
  100.  
  101. (defcustom resize-minibuffer-frame-max-height nil
  102.   "*Maximum size the minibuffer frame is allowed to become.
  103. If less than 1 or not a number, there is no limit.")
  104.  
  105. (defcustom resize-minibuffer-frame-exactly nil
  106.   "*If non-`nil', make minibuffer frame exactly the size needed to display all its contents.
  107. Otherwise, the minibuffer frame can temporarily increase in size but
  108. never get smaller while it is active."
  109.   :type 'boolean
  110.   :group 'resize-minibuffer)
  111.  
  112.  
  113. ;;;###autoload
  114. (defun resize-minibuffer-mode (&optional prefix)
  115.   "Enable or disable resize-minibuffer mode.
  116. A negative prefix argument disables this mode.  A positive argument or
  117. argument of 0 enables it.
  118.  
  119. When this minor mode is enabled, the minibuffer is dynamically resized to
  120. contain the entire region of text put in it as you type.
  121.  
  122. The variable `resize-minibuffer-mode' is set to t or nil depending on
  123. whether this mode is active or not.
  124.  
  125. The maximum height to which the minibuffer can grow is controlled by the
  126. variable `resize-minibuffer-window-max-height'.
  127.  
  128. The variable `resize-minibuffer-window-exactly' determines whether the
  129. minibuffer window should ever be shrunk to make it no larger than needed to
  130. display its contents.
  131.  
  132. When using a window system, it is possible for a minibuffer to be the sole
  133. window in a frame.  Since that window is already its maximum size, the only
  134. way to make more text visible at once is to increase the size of the frame.
  135. The variable `resize-minibuffer-frame' controls whether this should be
  136. done.  The variables `resize-minibuffer-frame-max-height' and
  137. `resize-minibuffer-frame-exactly' are analogous to their window
  138. counterparts."
  139.   (interactive "p")
  140.   (or prefix (setq prefix 0))
  141.   (cond
  142.    ((>= prefix 0)
  143.     (setq resize-minibuffer-mode t))
  144.    (t
  145.     (setq resize-minibuffer-mode nil))))
  146.  
  147. (defun resize-minibuffer-setup ()
  148.   (cond
  149.    (resize-minibuffer-mode
  150.     (cond
  151.      ((and (not (eq 'tty (console-type)))
  152.        (eq 'only (plist-get (frame-properties) 'minibuffer)))
  153.       (and resize-minibuffer-frame
  154.        (progn
  155.          (make-local-hook 'minibuffer-exit-hook)
  156.          (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore
  157.                nil t)
  158.          (make-local-hook 'post-command-hook)
  159.          (add-hook 'post-command-hook 'resize-minibuffer-frame nil t)
  160.          (unless (and (boundp 'icomplete-mode)
  161.               icomplete-mode)
  162.            (resize-minibuffer-frame)))))
  163.      (t
  164.       (make-local-hook 'post-command-hook)
  165.       (add-hook 'post-command-hook 'resize-minibuffer-window nil t)
  166.       (unless (and (boundp 'icomplete-mode)
  167.            icomplete-mode)
  168.     (resize-minibuffer-window)))))))
  169.  
  170. (defun resize-minibuffer-count-window-lines (&optional start end)
  171.   "Return number of window lines occupied by text in region.
  172. The number of window lines may be greater than the number of actual lines
  173. in the buffer if any wrap on the display due to their length.
  174.  
  175. Optional arguments START and END default to point-min and point-max,
  176. respectively."
  177.   (or start (setq start (point-min)))
  178.   (or end   (setq end   (point-max)))
  179.   (if (= start end)
  180.       0
  181.     (save-excursion
  182.       (save-restriction
  183.         (widen)
  184.     (narrow-to-region start end)
  185.     (goto-char start)
  186.         (vertical-motion (buffer-size))))))
  187.  
  188.  
  189. ;; Resize the minibuffer window to contain the minibuffer's contents.
  190. ;; The minibuffer must be the current window.
  191. (defun resize-minibuffer-window ()
  192.   (let ((height (window-height))
  193.     (lines (1+ (resize-minibuffer-count-window-lines))))
  194.     (and (numberp resize-minibuffer-window-max-height)
  195.      (> resize-minibuffer-window-max-height 0)
  196.      (setq lines (min
  197.               lines
  198.               resize-minibuffer-window-max-height)))
  199.     (or (if resize-minibuffer-window-exactly
  200.         (= lines height)
  201.       (<= lines height))
  202.     (enlarge-window (- lines height)))))
  203.  
  204.  
  205. ;; Resize the minibuffer frame to contain the minibuffer's contents.
  206. ;; The minibuffer frame must be the current frame.
  207. (defun resize-minibuffer-frame ()
  208.   (let ((height (frame-height))
  209.     (lines (1+ (resize-minibuffer-count-window-lines))))
  210.     (and (numberp resize-minibuffer-frame-max-height)
  211.      (> resize-minibuffer-frame-max-height 0)
  212.      (setq lines (min
  213.               lines
  214.               resize-minibuffer-frame-max-height)))
  215.     (cond
  216.      ((> lines height)
  217.       (set-frame-size (selected-frame) (frame-width) lines))
  218.      ((and resize-minibuffer-frame-exactly
  219.        (> height (plist-get minibuffer-frame-plist 'height))
  220.        (< lines height))
  221.       (set-frame-size (selected-frame) (frame-width) lines)))))
  222.  
  223. ;; Restore the original height of the frame.
  224. (defun resize-minibuffer-frame-restore ()
  225.   (set-frame-size (selected-frame) (frame-width)
  226.           (plist-get minibuffer-frame-plist 'height)))
  227.  
  228.  
  229. (provide 'rsz-minibuf)
  230.  
  231. (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
  232.  
  233. ;;; rsz-minibuf.el ends here
  234.